#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_PAYLOAD_SIZE 8
typedef struct
{
	int logical_channel_id;
	int physical_channel_id;
	unsigned char payload[MAX_PAYLOAD_SIZE];
	int SQ;
	int MFI;
} Frame;

void generate_payload_data(unsigned char *payload, int payload_size, int data_source)
{
	if (data_source == 0)
	{
		*payload = 0;
		for (int i = 0; i < payload_size; i++)
		{
			payload[i] = rand() % 2;
		}
		for (int i = 0; i < payload_size; i++)
		{
			printf("%d", payload[i]);
		}
	}
	else
	{
	}
}
void generate_data_file(int logical_channel_id, int physical_channel_id, long bandwidth, int S_Q, int MFI, int data_source)
{
	// printf("%d,%d,%d\n",logical_channel_id,physical_channel_id,data_source);
	unsigned char payload[MAX_PAYLOAD_SIZE];
	Frame frame;
	frame.SQ = S_Q;
	frame.MFI = MFI;
	frame.logical_channel_id = logical_channel_id;
	frame.physical_channel_id = physical_channel_id;
	generate_payload_data(frame.payload, sizeof(frame.payload), data_source);

	FILE *file;
	char filename[50];
	sprintf(filename, "DataFile_%d_%d_%d.dat", logical_channel_id, physical_channel_id, MFI);
	file = fopen(filename, "wb");

	if (file != NULL)
	{
		fwrite(&frame, sizeof(Frame), 1, file);
		fclose(file);
		printf("Generated data file: %s\n", filename);
	}
	else
	{
		printf("Failed to create data file.\n");
	}
	return;
}
void read_combined_data_file()
{
	int LID, PID, MFI;
	printf("请输入您像查询的数据文件\n");
	printf("逻辑通道id\n:");
	scanf("%d", &LID);
	printf("物理通道id\n:");
	scanf("%d", &PID);
	printf("时间标志位\n:");
	scanf("%d", &MFI);
	char filename[50];
	sprintf(filename, "DataFile_%d_%d_%d.dat", LID, PID, MFI);
	FILE *file = fopen(filename, "rb");
	if (file == NULL)
	{
		printf("Failed to open the combined data file.\n");
		return;
	}
	Frame frame;
	while (fread(&frame, sizeof(Frame), 1, file) == 1)
	{
		printf("Logical Channel ID: %d\n", frame.logical_channel_id);
		printf("Physical Channel ID: %d\n", frame.physical_channel_id);
		printf("Payload: ");
		for (int i = 0; i < sizeof(frame.payload); i++)
		{
			printf("%d", frame.payload[i]);
		}
		printf("\n");
		printf("SQ: %d\n", frame.SQ);
		printf("MFI: %d\n", frame.MFI);
		printf("\n");
	}

	fclose(file);
}
int main()
{
	srand(time(0));
	int MFI = 0;
	int data_source = 0;
	int logical_channel_id = 0;
	int physical_channel_id = 0;
	long vc4_bandwidth = 2500000000;
	long vc12_bandwidth = 500000000;
	char order[10];
	char source[10];
	int logical_channel_sign = 0;
	printf("请选择通道组成类型，注意：可选类型有，VC-4,VC-12,combination\n");
	printf("如果您想读取数据的话请输入read\n");
	printf("输入：");
	scanf("%s", order);
	printf("请选择数据源类型，注意：可选类型有：纯随机八位二进制数（rand），有规律八位二进制数(regul)\n");
	printf("输入：");
	scanf("%s", source);
	if (strcmp(source, "rand") == 0)
	{
		data_source = 0;
	}
	else if (strcmp(source, "regul") == 0)
	{
		data_source = 1;
	}
	else
	{
		printf("输入类型错误！");
		main();
	}

	if (strcmp(order, "combination") == 0)
	{
		int n, a_sum, b_sum, a_count, b_count;
		n = rand() % 64 + 1;

		a_sum = n;
		b_sum = (64 - n) * 63;
		srand(time(0));
		a_count = rand() % (a_sum + 1); // 生成0到a_sum之间的随机数
		b_count = rand() % (b_sum + 1); // 生成0到b_sum之间的随机数
		int a_array[a_count];
		int b_array[b_count];

		for (int i = 0; i < a_count; i++)
		{
			if (i == a_count - 1)
			{
				a_array[i] = a_sum;
			}
			else
			{
				int max_random_a = a_sum - (a_count - i); // 随机数上限为剩余总和减去剩余数量之和
				int a = rand() % (max_random_a + 1) + 1;  // 随机生成一个小于等于max_random_a的数
				a_sum -= a;
				a_array[i] = a;
			}
		}
		for (int i = 0; i < b_count; i++)
		{
			if (i == b_count - 1)
			{
				b_array[i] = b_sum;
			}
			else
			{
				int max_random_b = b_sum - (b_count - i); // 随机数上限为剩余总和减去剩余数量之和
				int b = rand() % (max_random_b + 1) + 1;  // 随机生成一个小于等于max_random_b的数
				b_sum -= b;								  // 更新b的总和
				b_array[i] = b;
			}
		}
		char mode[10];
		printf("选择数据发送模式：模式one，每个通道发送一个数据，MFI默认为0；模式two，每个通道无限发送数据\n");
		printf("请输入数据发送模式one or two：");
		scanf("%s", mode);
		printf("\n");
		if (strcmp(mode, "one") == 0)
		{
			for (int j = 0; j < a_count; j++)
			{
				for (int i = 0; i < a_array[j]; i++)
				{
					generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth, i, 0, data_source);
					physical_channel_id++;
				}
				logical_channel_id++;
			}
			for (int j = 0; j < b_count; j++)
			{
				for (int i = 0; i < b_array[j]; i++)
				{
					generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth, i, 0, data_source);
					physical_channel_id++;
				}
				logical_channel_id++;
			}
		}
		else if (strcmp(mode, "two") == 0)
		{
			for (MFI = 0; MFI <= 4095; MFI++)
			{
				for (int j = 0; j < a_count; j++)
				{
					for (int i = 0; i < a_array[j]; i++)
					{
						generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth, i, MFI, data_source);
						physical_channel_id++;
					}
					logical_channel_id++;
				}
				for (int j = 0; j < b_count; j++)
				{
					for (int i = 0; i < b_array[j]; i++)
					{
						generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth, i, MFI, data_source);
						physical_channel_id++;
					}
					logical_channel_id++;
				}
				logical_channel_id = 0;
				physical_channel_id = 0;
				if (MFI == 4095)
				{
					MFI = 0;
				}
			}
		}
		else
		{
			printf("输入模式错误！");
		}
	}
	else if (strcmp(order, "VC-4") == 0)
	{
		char mode[10];
		printf("选择数据发送模式：模式one，每个通道发送一个数据，MFI默认为0；模式two，每个通道无限发送数据\n");
		printf("请输入数据发送模式one or two：");
		scanf("%s", mode);
		printf("\n");
		if (strcmp(mode, "two") == 0)
		{
			for (MFI = 0; MFI <= 4095; MFI++)
			{
				for (physical_channel_id = 0; physical_channel_id <= 6; physical_channel_id++)
				{
					logical_channel_id = physical_channel_id;
					generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth, 1, MFI, data_source);
				}
				if (MFI == 4095)
				{
					MFI = 0;
				}
			}
		}
		else if (strcmp(mode, "one") == 0)
		{
			for (physical_channel_id = 0; physical_channel_id <= 6; physical_channel_id++)
			{
				logical_channel_id = physical_channel_id;
				generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth, 1, MFI, data_source);
			}
		}
		else
		{
			printf("输入模式错误！");
		}
	}
	else if (strcmp(order, "VC-12") == 0)
	{
		char mode[10];
		printf("选择数据发送模式：模式one，每个通道发送一个数据，MFI默认为0；模式two，每个通道无限发送数据\n");
		printf("请输入数据发送模式one or two：");
		scanf("%s", mode);
		printf("\n");
		if (strcmp(mode, "two") == 0)
		{
			for (MFI = 0; MFI <= 4095; MFI++)
			{
				for (physical_channel_id = 0; physical_channel_id <= 4032; physical_channel_id++)
				{
					logical_channel_id = physical_channel_id;
					generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth, 1, 0, data_source);
				}
				if (MFI == 4095)
				{
					MFI = 0;
				}
			}
		}
		else if (strcmp(mode, "one") == 0)
		{
			for (physical_channel_id = 0; physical_channel_id <= 4032; physical_channel_id++)
			{
				logical_channel_id = physical_channel_id;
				generate_data_file(logical_channel_id, physical_channel_id, vc4_bandwidth, 1, 0, data_source);
			}
		}
		else
		{
			printf("输入模式错误！");
		}
	}
	else if (strcmp(order, "read") == 0)
	{
		int file_count = 64;
		// combine_data_files(file_count);
		read_combined_data_file();
	}
	else
	{
		printf("无效输入啊哥们!\n");
	}
	printf("\n");
	main();
	return 0;
}
